<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="背景最近在Windows 10上使用Linux子系统，发现它有一个非常坑爹的特点：Linux子系统是没有开机关机状态的，每次进入Bash shell就自动载入，退出后Linux子系统的所有进程都会被关闭，如果你撞了Mysql之类的服务要想随时运行的话就要保持Bash shell的随时开启，更坑的是这些服务并不会随之进入Bash shell而自动启动， 我只好写一个Python脚本用于管理这些服务。">
<meta property="og:type" content="article">
<meta property="og:title" content="【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本">
<meta property="og:url" content="http://yoursite.com/2017/03/31/【Python运维】最简单的Python运维脚本/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="背景最近在Windows 10上使用Linux子系统，发现它有一个非常坑爹的特点：Linux子系统是没有开机关机状态的，每次进入Bash shell就自动载入，退出后Linux子系统的所有进程都会被关闭，如果你撞了Mysql之类的服务要想随时运行的话就要保持Bash shell的随时开启，更坑的是这些服务并不会随之进入Bash shell而自动启动， 我只好写一个Python脚本用于管理这些服务。">
<meta property="og:updated_time" content="2017-03-31T14:41:22.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本">
<meta name="twitter:description" content="背景最近在Windows 10上使用Linux子系统，发现它有一个非常坑爹的特点：Linux子系统是没有开机关机状态的，每次进入Bash shell就自动载入，退出后Linux子系统的所有进程都会被关闭，如果你撞了Mysql之类的服务要想随时运行的话就要保持Bash shell的随时开启，更坑的是这些服务并不会随之进入Bash shell而自动启动， 我只好写一个Python脚本用于管理这些服务。">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-【Python运维】最简单的Python运维脚本" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2017/03/31/【Python运维】最简单的Python运维脚本/" class="article-date">
  <time datetime="2017-03-31T14:24:15.000Z" itemprop="datePublished">2017-03-31</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h1><p>最近在Windows 10上使用Linux子系统，发现它有一个非常坑爹的特点：Linux子系统是没有开机关机状态的，每次进入Bash shell就自动载入，退出后Linux子系统的所有进程都会被关闭，如果你撞了Mysql之类的服务要想随时运行的话就要保持Bash shell的随时开启，更坑的是这些服务并不会随之进入Bash shell而自动启动， 我只好写一个Python脚本用于管理这些服务。</p>
<h1 id="相关技术"><a href="#相关技术" class="headerlink" title="相关技术"></a>相关技术</h1><p>Python3, argparse module</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">from</span> os <span class="keyword">import</span> system</div><div class="line"><span class="keyword">from</span> argparse <span class="keyword">import</span> ArgumentParser</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">start_service</span><span class="params">(service)</span>:</span></div><div class="line">    system(<span class="string">"service &#123;&#125; start"</span>.format(service))</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">stop_service</span><span class="params">(service)</span>:</span></div><div class="line">    system(<span class="string">"service &#123;&#125; stop"</span>.format(service))</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">restart_service</span><span class="params">(service)</span>:</span></div><div class="line">    print(service)</div><div class="line">    system(<span class="string">"service &#123;&#125; restart"</span>.format(service))</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">manage_service</span><span class="params">()</span>:</span></div><div class="line">    services = []</div><div class="line">    services.append(<span class="string">"xinetd"</span>)</div><div class="line">    services.append(<span class="string">"lighttpd"</span>)</div><div class="line">    <span class="keyword">return</span> services</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">set_args</span><span class="params">()</span>:</span></div><div class="line">    parser = ArgumentParser()</div><div class="line">    parser.add_argument(<span class="string">"service"</span>, help = <span class="string">"the service to be managed."</span>)</div><div class="line">    parser.add_argument(<span class="string">"-s"</span>, <span class="string">"--start"</span>, help = <span class="string">"start the service(s)."</span>, action = <span class="string">"store_true"</span>)</div><div class="line">    parser.add_argument(<span class="string">"-r"</span>, <span class="string">"--restart"</span>, help = <span class="string">"restart the service(s)."</span>, action = <span class="string">"store_true"</span>)</div><div class="line">    parser.add_argument(<span class="string">"-p"</span>, <span class="string">"--stop"</span>, help = <span class="string">"stop the service(s)."</span>, action = <span class="string">"store_true"</span>)</div><div class="line">    <span class="keyword">return</span> parser.parse_args()</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">deal</span><span class="params">(args,services)</span>:</span></div><div class="line">    <span class="keyword">global</span> start_service, restart_service, stop_service</div><div class="line">    services = services <span class="keyword">if</span> <span class="keyword">not</span> args.service <span class="keyword">else</span> services <span class="keyword">if</span> args.service == <span class="string">"all"</span><span class="keyword">else</span> [args.service]</div><div class="line">    operation = start_service <span class="keyword">if</span> args.start <span class="keyword">else</span> restart_service <span class="keyword">if</span> args.restart <span class="keyword">else</span> stop_service</div><div class="line">    <span class="keyword">for</span>  service <span class="keyword">in</span> services:</div><div class="line">       operation(service)</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</div><div class="line">    deal(set_args(),manage_service())</div></pre></td></tr></table></figure>
<h1 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h1><h2 id="开启服务"><a href="#开启服务" class="headerlink" title="开启服务"></a>开启服务</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">(env) root@DESKTOP-1DDIIV2:~# python pyops.py all -s</div><div class="line">initctl: 无法连接到 Upstart: Failed to connect to socket /com/ubuntu/upstart: 拒绝连接</div><div class="line"> * Starting internet superserver xinetd                                                                                                                          [fail]</div><div class="line"> * Starting web server lighttpd                                                                                                                                  [ OK ]</div><div class="line">(env) root@DESKTOP-1DDIIV2:~#</div></pre></td></tr></table></figure>
<h2 id="关闭服务"><a href="#关闭服务" class="headerlink" title="关闭服务"></a>关闭服务</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">(env) root@DESKTOP-1DDIIV2:~# python pyops.py xinetd -p</div><div class="line">initctl: 无法连接到 Upstart: Failed to connect to socket /com/ubuntu/upstart: 拒绝连接</div><div class="line"> * Stopping internet superserver xinetd                                                                                                                          [ OK ]</div><div class="line">(env) root@DESKTOP-1DDIIV2:~#</div></pre></td></tr></table></figure>
<h2 id="重启服务"><a href="#重启服务" class="headerlink" title="重启服务"></a>重启服务</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">(env) root@DESKTOP-1DDIIV2:~# python pyops.py xinetd -r</div><div class="line">xinetd</div><div class="line">initctl: 无法连接到 Upstart: Failed to connect to socket /com/ubuntu/upstart: 拒绝连接</div><div class="line"> * Stopping internet superserver xinetd                                                                                                                          [ OK ]</div><div class="line"> * Starting internet superserver xinetd                                                                                                                          [ OK ]</div><div class="line">(env) root@DESKTOP-1DDIIV2:~#</div></pre></td></tr></table></figure>
<h1 id="感想"><a href="#感想" class="headerlink" title="感想"></a>感想</h1><p>Python脚本还是比shell脚本好写很多啊。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2017/03/31/【Python运维】最简单的Python运维脚本/" data-id="cj0xxtlyv00003sulz8qajq9w" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2017/03/31/Modern-C-现代C-中优雅的异步操作/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">[Modern C++]现代C++中优雅的异步操作</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">March 2017</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2017/03/31/【Python运维】最简单的Python运维脚本/">【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本</a>
          </li>
        
          <li>
            <a href="/2017/03/31/Modern-C-现代C-中优雅的异步操作/">[Modern C++]现代C++中优雅的异步操作</a>
          </li>
        
          <li>
            <a href="/2017/03/31/动态修改Python对象/">动态修改Python对象</a>
          </li>
        
          <li>
            <a href="/2017/03/31/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>

  </div>
</body>
</html>